﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using DataBase;
using LinqToDB;
using System.Collections.ObjectModel;

namespace ScheduleGenerator
{
    /// <summary>
    /// Логика взаимодействия для MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private ScheduleDB _db;

        public MainWindow()
        {
            InitializeComponent();

            _db = new ScheduleDB();

            SetControlsBinding();
        }

        private void SetControlsBinding()
        {
            ListBoxFaculties.ItemsSource = new ObservableCollection<Faculty>(_db.Faculties);
        }

        private void ButtonUpdateFaculty_Click(object sender, RoutedEventArgs e)
        {
            var faculty = (Faculty)GridEditFaculty.DataContext;

            _db.Update(faculty);
        }

        private void ButtonAddFaculty_Click(object sender, RoutedEventArgs e)
        {
            var collection = (ObservableCollection<Faculty>)ListBoxFaculties.ItemsSource;

            var newFaculty = new Faculty() { Id = Guid.NewGuid(), Name = "Имя факультета" };

            _db.Insert(newFaculty);
            collection.Add(newFaculty);
        }

        private void ButtonRemoveFaculty_Click(object sender, RoutedEventArgs e)
        {
            var collection = (ObservableCollection<Faculty>)ListBoxFaculties.ItemsSource;

            var removedFaculty = (Faculty)ListBoxFaculties.SelectedItem;

            if (removedFaculty == null)
            {
                MessageBox.Show("lol");
                return;
            }

            _db.Delete(removedFaculty);
            collection.Remove(removedFaculty);
        }
    }
}
